using System;
using System.Drawing;
using System.Windows.Forms;

namespace demo
{
    /// <summary>
    /// 患者数据查询结果显示窗体
    /// </summary>
    public partial class PatientDataResultForm : Form
    {
        private TableLayoutPanel tableLayoutPanel;
        private Button btnClose;
        private Label lblTitle;
        private Panel contentPanel;

        public PatientDataResultForm()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();

            // 窗体设置
            this.Text = "患者数据查询结果";
            this.Size = new Size(500, 400);
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ShowInTaskbar = false;

            // 标题标签
            lblTitle = new Label
            {
                Text = "患者信息",
                Font = new Font("微软雅黑", 12F, FontStyle.Bold),
                ForeColor = Color.DarkBlue,
                TextAlign = ContentAlignment.MiddleCenter,
                Dock = DockStyle.Top,
                Height = 40,
                BackColor = Color.LightBlue
            };
            this.Controls.Add(lblTitle);

            // 内容面板
            contentPanel = new Panel
            {
                Dock = DockStyle.Fill,
                Padding = new Padding(10),
                BackColor = Color.White
            };
            this.Controls.Add(contentPanel);

            // 关闭按钮
            btnClose = new Button
            {
                Text = "关闭",
                Size = new Size(80, 30),
                Anchor = AnchorStyles.Bottom | AnchorStyles.Right,
                UseVisualStyleBackColor = true
            };
            btnClose.Location = new Point(this.Width - btnClose.Width - 20, this.Height - btnClose.Height - 50);
            btnClose.Click += BtnClose_Click;
            this.Controls.Add(btnClose);

            this.ResumeLayout(false);
        }

        /// <summary>
        /// 显示患者数据
        /// </summary>
        /// <param name="response">查询响应结果</param>
        public void ShowPatientData(PatientDataResponse response)
        {
            contentPanel.Controls.Clear();

            if (response == null)
            {
                ShowErrorMessage("响应数据为空");
                return;
            }

            if (!response.Success)
            {
                ShowErrorMessage(response.Message ?? "查询失败");
                return;
            }

            if (response.Data == null)
            {
                ShowErrorMessage("未找到患者数据");
                return;
            }

            // 创建表格布局显示患者信息
            tableLayoutPanel = new TableLayoutPanel
            {
                Dock = DockStyle.Fill,
                ColumnCount = 2,
                AutoSize = true,
                CellBorderStyle = TableLayoutPanelCellBorderStyle.Single,
                BackColor = Color.White
            };

            // 设置列宽
            tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));
            tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 70F));

            var data = response.Data;
            int row = 0;

            // 添加数据行
            AddDataRow("就诊流水号", data.OutpVisitId, row++);
            AddDataRow("患者ID", data.PatientId, row++);
            AddDataRow("患者姓名", data.PatientName, row++);
            AddDataRow("专科名称", data.OutpSpecialName, row++);
            AddDataRow("就诊日期", data.OutpDate, row++);
            AddDataRow("就诊次数", data.VisitId, row++);
            AddDataRow("接诊医生", data.AdmissionDoctor, row++);
            AddDataRow("接诊流水号", data.DoctorVsOutpVisitId, row++);
            AddDataRow("二维码", data.QrCode, row++);
            AddDataRow("挂号费状态", data.RegistrationFeeStatus, row++);
            AddDataRow("患者级别", data.PatLevel, row++);
            AddDataRow("检验数量", data.LisCount, row++);
            AddDataRow("检查数量", data.ExamCount, row++);
            AddDataRow("读取标志", data.ReadFlag, row++);
            AddDataRow("医嘱名称", data.OrderNames, row++);

            contentPanel.Controls.Add(tableLayoutPanel);
        }

        private void AddDataRow(string label, string value, int row)
        {
            tableLayoutPanel.RowCount = row + 1;
            tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));

            // 标签
            var lblField = new Label
            {
                Text = label + ":",
                Font = new Font("微软雅黑", 9F, FontStyle.Bold),
                ForeColor = Color.DarkBlue,
                TextAlign = ContentAlignment.MiddleLeft,
                Dock = DockStyle.Fill,
                Padding = new Padding(5),
                BackColor = Color.LightGray
            };
            tableLayoutPanel.Controls.Add(lblField, 0, row);

            // 值
            var lblValue = new Label
            {
                Text = value ?? "N/A",
                Font = new Font("微软雅黑", 9F),
                ForeColor = Color.Black,
                TextAlign = ContentAlignment.MiddleLeft,
                Dock = DockStyle.Fill,
                Padding = new Padding(5),
                BackColor = Color.White
            };
            tableLayoutPanel.Controls.Add(lblValue, 1, row);
        }

        private void ShowErrorMessage(string message)
        {
            var lblError = new Label
            {
                Text = message,
                Font = new Font("微软雅黑", 10F),
                ForeColor = Color.Red,
                TextAlign = ContentAlignment.MiddleCenter,
                Dock = DockStyle.Fill
            };
            contentPanel.Controls.Add(lblError);
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}